var api = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        column: '',
        u2_id: '',
        u_id: '',
        aennState: '',
        userArticleList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var a = this, u_id = wx.getStorageSync('userData').u_id, page = a.data.page;
        a.data.u2_id = options.u_id, a.data.u_id = u_id;
        if (u_id == options.u_id){
            wx.switchTab({ url: '../mine/mine' })
        }else{
            a.mineLoad(u_id, options.u_id), a.userArticleList(options.u_id, page);
        }
    },
    mineLoad: function (u_id, follow_id) {
        var a = this;
        wx.showLoading({ title: "加载中" });
        wx.request({
            url: api.globalData.Api + '/userInfo',
            method: 'GET',
            data: {
                u_id: u_id,
                follow_id: follow_id
            },
            success: function (res) {
                if (res.data.status == 1) {
                    a.data.aennState = res.data.data.state;
                    a.setData({
                        userData: res.data.data,
                        aennState: res.data.data.state
                    });
                }
            }
        });
    },
    // 用户制作的文章列表
    userArticleList: function (u_id, page){
        var a = this;
        wx.showLoading({ title: "加载中" });
        wx.request({
            url: api.globalData.Api + '/userArticleList',
            method: 'GET',
            data: {
                u_id: u_id,
                page: page
            },
            success: function (res) {
                if (res.data.status == 1) {
                    var r = res.data.data.data;
                    if(page > 1){
                        if (r.length == 0){
                            wx.showToast({ title: ' 已全部加载完成！' });
                        }else {
                            a.setData({
                                userArticleList: a.data.userArticleList.concat(r),
                                page: page
                            }), wx.hideLoading();
                        }
                    }else {
                        a.data.userArticleList = r;
                        a.setData({
                            userArticleList: r
                        }), wx.hideLoading();
                    }
                    
                }
            }
        });
    },

    clickArticle: function (o) {
        var u2_id = this.data.u2_id, a_id = o.currentTarget.dataset.a_id;
        wx.navigateTo({
            url: '../article/article?article_id=' + a_id + '&user_id=' + u2_id
        })
    },
    //  跳转到粉丝页面
    toFnas: function () {
        wx.navigateTo({
            url: "../me-fans/me-fans?fans_id=" + this.data.u2_id + "&u_id=" + this.data.u_id
        });
    },
    //  跳转到关注页面
    toFollow: function () {
        wx.navigateTo({
            url: "../me-attention/me-attention?follow_id=" + this.data.u2_id + "&u_id=" + this.data.u_id
        });
    },

    toggleAttention: function(){
        var a = this;
        wx.request({
            url: api.globalData.Api + '/followUser',
            data: {
                u1_id: a.data.u_id,
                u2_id: a.data.u2_id
            },
            method: 'GET',
            dataType: 'json',
            success: function (res) {
                if (res.data.status == 1) {
                    if (a.data.aennState == 1) {
                        wx.showToast({ title: '取消关注成功' });
                        a.setData({ aennState: 0 })
                    } else {
                        wx.showToast({ title: '关注成功' });
                        a.setData({ aennState: 1 });
                    }
                }
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var a = this, u2_id = a.data.u2_id, page = a.data.page + 1;
        a.userArticleList(u2_id, page);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})